exports.CreateOrderItems=function(_args)
{
	
	//Ti.include('cs/accounting.js')
	var accounting=require('cs/accounting');
	var pathimage=Ti.App.Properties.getString('pathimage');
	var canscrool=0
	var money=0;
	var view = Ti.UI.createView({
		width:_args.width,
		height:_args.height,
		backgroundColor: _args.backgroundColor,
		top:0,
		left:_args.left,
	})
	
	
	
	var header_height=40;
	
	var lb_mes = Ti.UI.createLabel({
	  	text:'Chưa có thực đơn',
	  	color:'Black',
	  	font:{
				fontSize:20,
				fontWeight:'bold',
		},
	});
	view.add(lb_mes);
	
	///////////header view////////
	/*
	var header = Ti.UI.createView({
		width:_args.width,
		height:header_height,
		backgroundColor: 'white',
		top:0,
		left:0,
		
	})
	view.add(header)
	
	var lb1 = Ti.UI.createLabel({
	  	text:'Bàn số: '+_args.OrderName,
	  	color:'Black',
	  	font:{
				fontSize:Ti.App.Properties.getInt('fontsize_header'),
				fontWeight:'bold',
		},
	});
	header.add(lb1);
	*/
	////////////////////////////
	var platformWidth=Titanium.Platform.displayCaps.platformWidth;//Ti.App.Properties.getInt('platformWidth');
	var platformHeight=Titanium.Platform.displayCaps.platformHeight;//Ti.App.Properties.getInt('platformHeight');
	var bt_height=Ti.App.Properties.getInt('Order_buttonnumber_height')
	///////////Table view////////
	var tableview = Ti.UI.createTableView({
		width:_args.width,
		height:platformHeight-(bt_height+header_height),
		backgroundColor: 'Transparent',
		top:0,//header.height,
		left:0,
		separatorColor:'Transparent',
		scrollable:true,
	})
	tableview.addEventListener('scroll', function (e) {
		if(canscrool==1)
		{
			canscrool=0
			var index=0
			switch (Titanium.Platform.osname)
			{
			    case 'android': 
			        index=tableview.data.length-1
			        break;
			    case 'ipad':
			        index=tableview.data[0].rowCount-1
			        break;
			}
			//Ti.API.info ('Scrool????       '+index)
			lb_TienTraValue.text=accounting.formatMoney(money, "", 0)  ;
			e.source.scrollToIndex(index,{
			    //position:Titanium.UI.iPhone.TableViewScrollPosition.MIDDLE,
			    animated:true
			  }
			);
		}
    })
	view.add(tableview)
	//////////////////Thanh toan//////////

	var bt_DongPhieu = Titanium.UI.createButton({
		 borderColor: Ti.App.Properties.getString('borderColor'),
	     borderRadius: Ti.App.Properties.getInt('borderRadius'),
	     borderWidth:Ti.App.Properties.getInt('borderWidth'),
		 title:'Đóng phiếu',
		 backgroundImage:pathimage+'bt_red.png',
		 backgroundSelectedImage:pathimage+'bt_red_press.png',
		 width:150,
	 	 height:	bt_height,
	   	 bottom:0,
	   	 right:0,
	   	 font:{
			fontSize:20,
			fontWeight:'bold',
	 	 },
		}); 
	bt_DongPhieu.addEventListener('click', function (e) {
		Ti.App.fireEvent('back_'+_args.idWinParent)	
    })
	view.add(bt_DongPhieu)
	
	var bt_ThanhToan = Titanium.UI.createButton({
		 borderColor: Ti.App.Properties.getString('borderColor'),
	     borderRadius: Ti.App.Properties.getInt('borderRadius'),
	     borderWidth:Ti.App.Properties.getInt('borderWidth'),
		 color:'Black',
		 title:'Thanh toán',
		 backgroundImage:pathimage+'bg_blue.png',
		 backgroundSelectedImage:pathimage+'bt_Nau_press.png',
		 width:_args.width-(bt_DongPhieu.width),
	 	 height:	bt_height,
	   	 bottom:0,
	   	 left:0,
	   	 font:{
			fontSize:28,
			fontWeight:'bold',
	 	 },
	}); 

	bt_ThanhToan.addEventListener('click', function (e) {
	//	Ti.API.info ('<<<<<<<<<:'+tableview.getData())
		Ti.App.fireEvent('OrderWindow:Cash',{data:tableview.getData()})	
    })
	view.add(bt_ThanhToan)

	
	///////////TIEN PHAI TRA////////
	var footer = Ti.UI.createView({
		width:_args.width,
		height:header_height,
		backgroundColor: 'Transparent',
		bottom:bt_ThanhToan.height,
		left:0,
		
	})
	view.add(footer)
	var lb_TienTra = Ti.UI.createLabel({
		left:5,
	  	text:'Bàn '+_args.OrderName+' :',
	  	color:'Red',
	  	font:{
				fontSize:20,
				fontWeight:'bold',
		},
	});
	footer.add(lb_TienTra);
	var lb_TienTraValue = Ti.UI.createLabel({
		right:5,
	  	text: accounting.formatMoney(0, "", 0),
	  	color:'Red',
	  	font:{
				fontSize:20,
				fontWeight:'bold',
		},
	});
	footer.add(lb_TienTraValue);
	////////////////////////////
	
	
	
	var CreateTableViewRowAppend = function(e) {
		var item=e;
		if(e.isloaded==1)
			item=e.data;
		Ti.API.info ('Name__:'+item.Name)
		var row = Ti.UI.createTableViewRow({
	            height:Ti.App.Properties.getInt('Order_buttonnumber_height'),//	Ti.App.Properties.getInt('button_height'),
				Id:item.Id,
				Name:item.Name,
		})
		var lbQuantity = Ti.UI.createLabel({
			left:5,
			width:30,
		  	text:item.Quantity,
		  	color:'Black',
		  	font:{
					fontSize:20,
					//fontWeight:'bold',
			},
		});
		
		var lbName = Ti.UI.createLabel({
			left:lbQuantity.width,
			right:100,
		  	text:item.Name,
		  	color:'Black',
		  	font:{
					fontSize:20,
					//fontWeight:'bold',
			},
		});
		var lbPrice_Amount = Ti.UI.createLabel({
			right:5,
		  	text:accounting.formatMoney(item.Price_Amount, "", 0)  ,
		  	color:'Black',
		  	font:{
					fontSize:20,
					//fontWeight:'bold',
			},
		});
		
		var vi_line = Titanium.UI.createView({
			bottom:0,
			width:'100%',
			height:2,
			backgroundImage: pathimage+'img_devide_line_setting.png',
		 	touchEnabled: false,
		}); 
		row.add(lbName);
		row.add(vi_line);
		row.add(lbQuantity);
		row.add(lbPrice_Amount);
		return row;
	};
	
	_args.egc.addEventListener('OrderWindow:SelectItems', function (e) {
		
		//var ticket=require('db').selectTickets(_args.);
		var insert=require('db').insertTicketItems(_args.TablesId,e.Item,e.Quantity);
		
		
		//Ti.API.info ('TablesId'+_args.TablesId)
		
		lb_mes.text='';
		var row=CreateTableViewRowAppend({
			  Id:e.Id,
			  Name:e.Name,
			  Quantity:e.Quantity,
			  Price_Amount:e.Price_Amount
		});
		
		tableview.appendRow(row,{});
		
		
		canscrool=1;
		money=money+(e.Quantity*e.Price_Amount);
		tableview.fireEvent('scroll');
	
		
	})
	
	
	
	///////////////////////LOADED//////////////////////////////////////////////
	var LoadTicketItemsDemo_Data = function(TicketId) {
 		var json=require('db').selectTicketItems(TicketId);
		return json;
    }
    var LoadTicketItems = function() {
    	//Ti.API.info ('TicketId:'+_args.TicketId)
 		var tableData=[];
 		var json=[];
 		
 		if (Ti.App.Properties.getInt('IsDemo')==1) 
 		{
 			json=LoadTicketItemsDemo_Data(_args.TicketId);
 		}
 		else
 		{
 			//json=LoadListDataLocal(e);
 		}
 		
 		Ti.API.info ('So luong mon an:'+json.length)
 		/*
 		if (json.length==0) {
 			require('settings').Alert_error(win,Globals.connect_pop);
 		}else
 		*/
 		if(json.length!=0)
 		{
 			lb_mes.text='';
 		}
 		require('settings').LoadingData2TableViewNoScrool(tableview,json,CreateTableViewRowAppend);
 		
 		
 		var json_Tickets=_args.json_Tickets;
 		//Ti.API.info ('LLLL:'+json_Tickets.length)
 		if(json_Tickets.length!=0)
 		{
 			var TotalAmount=json_Tickets[0].TotalAmount;
	 		money=TotalAmount;
	 		lb_TienTraValue.text= accounting.formatMoney(TotalAmount, "", 0)  ;//TotalAmount;
 		}
 	
	};
	_args.egc.addEventListener('OrderItems:ChangeTableExcute', function (e) {
		//{TableId_New:TableId_New,TableName_New:TableName_New}
		lb_TienTra.text='Bàn '+e.TableName_New;
		_args.TicketId=e.TicketId_New
		_args.TablesId=e.TableId_New
		LoadTicketItems()
	})
	LoadTicketItems()
    
    
    
    
    
    
    
    
	
	return view;
}
